package cn.qinhao.pethome.basic.until;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.mail.internet.MimeMessage;
import java.io.File;

/*专门用于发送邮件的*/
@Component
public class EmailUtil {
    @Autowired//邮件的固定写法
    private  JavaMailSender javaMailSender;
    //创建一个公共静态工具对象，供其他类调用
    public static EmailUtil emailUtil;//静态属性属于本类，本类才可以调用本类的非静态方法
    @PostConstruct//初始化该对象
    public void  init(){
        emailUtil=this;
    }
    @Value("${email.sendperson}")
    private  String sendperson;
    @Value("${email.receivedperson}")
    private  String receivedperson;
    public  String send() throws Exception{
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom(sendperson);
        helper.setSubject("高级邮件发送");
        //第二个参数 true 代表是否是一个html内容
        helper.setText("<h1>审核成功</h1>"+
                "<img src='https://p0.ssl.img.360kuai.com/t0169a0e58e0c2a5f4e.webp' />",true);
        //添加附件
        helper.addAttachment("文件名.jpg",new File("E:\\java\\知识点回顾\\resources\\24.jpg"));
        helper.addAttachment("压缩文件", new File("E:\\java\\知识点回顾\\resources\\压缩包.zip"));
        //收件人
        helper.setTo(receivedperson);
        //发送邮件
        javaMailSender.send(mimeMessage);
        return "发送邮件成功";
    }
}
